home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9551 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  118 lines

  1. Path: newsfeed.direct.ca!usenet
  2. From: qjackson@direct.ca
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Where do I put inline functions?
  5. Date: Sat, 02 Mar 1996 17:29:10 GMT
  6. Organization: Parsepolis Software
  7. Message-ID: <4ha0bb$297@aphex.direct.ca>
  8. References: <4h7sj0$10a@mother.usf.edu>
  9. Reply-To: qjackson@direct.ca
  10. NNTP-Posting-Host: 204.174.251.87
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. millert@grad.csee.usf.edu (Timothy Miller) wrote:
  14.  
  15. >Should I put inline functions in .cpp files like normal functions or in 
  16. >.hpp files like #defines would be in C?
  17.  
  18. "Should" is a dangerous word when it comes to programming.... ;-)
  19.  
  20. A tolerable place to put them is in the .hpp file with the class
  21. definition, since they will be needed by the compiler for any file
  22. that includes the .hpp.  (A bad place to put them would be the .cpp,
  23. since they are compiled only once and then are no longer visible to
  24. the compiler -- how can the compiler include them inline?)
  25.  
  26. Some people put them in a file with an extension like .ipp and include
  27. that in the .hpp like this:
  28.  
  29. #ifndef FOO_H
  30. #define FOO_H
  31.  
  32. class foo
  33. {
  34.  
  35.     // ...
  36.  
  37. };
  38.  
  39. #include "foo.ipp"
  40.  
  41. #endif
  42.  
  43. Since inline functions are short and sweet, and not too hard to
  44. follow, a the "tolerable" approach yields:
  45.  
  46. #ifndef FOO_H
  47. #define FOO_H
  48.  
  49. class foo
  50. {
  51.  
  52.     void bar (void);
  53.  
  54.     // ...
  55.  
  56. };
  57.  
  58. inline 
  59. void foo::bar (void)
  60. {
  61.  
  62.     // do some bar
  63.  
  64. }
  65.  
  66. #endif
  67.  
  68. One of the beauties of the above -- if bar ever goes "outline", cut
  69. and paste to the .cpp is all that's required.
  70.  
  71. The "worst" approach looks something like this:
  72.  
  73. #ifndef FOO_H
  74. #define FOO_H
  75.  
  76. class foo
  77. {
  78.  
  79.     void bar (void)
  80.     {
  81.  
  82.         // Do some bar
  83.  
  84.     }
  85.  
  86.     // ...
  87.  
  88. };
  89.  
  90. #endif
  91.  
  92. Except for the very simplest functions, such as formal virtual dtors,
  93. the above clogs up the class definition and requires a bit of work to
  94. extract a memfunc should it ever evolve and require outlining.
  95.  
  96.  
  97. And, of course, your mileage may vary.
  98.  
  99.  
  100. Cheers,
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. --                           
  112.                            | 
  113.     Parsepolis Software    |   Quinn Tyler Jackson
  114.         "ParseCity"        |     (aka 'Jamshid')
  115. >--------------------------|   qjackson@direct.ca
  116.                            |---------------------->
  117.  
  118.